﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        internal static string GetFileSize(long bytes)
        {
            const long kilobyte = 1024;
            const long megabyte = 1024 * kilobyte;
            const long gigabyte = 1024 * megabyte;

            if (bytes >= gigabyte)
                return $"{bytes / (double)gigabyte:F2} GB";

            if (bytes >= megabyte)
                return $"{bytes / (double)megabyte:F2} MB";

            return $"{bytes / (double)kilobyte:F2} KB";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.Height = (int)(this.ClientRectangle.Height * 0.9);
            loadList();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 1)
            {
                // 获取被点击的单元格的值
                string cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                Process process = null;
                try
                {
                    process = Process.Start(cellValue);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    if (process != null)
                    {
                        process.Close(); // 释放进程资源
                    }
                }
                //System.Diagnostics.Process.Start(cellValue);
            }
        }
        class fileClass
        {
            public string fileName { get; set; }
            public string fileFullName { get; set; }
            public DateTime fileCreationTime { get; set; }
            public DateTime fileLastAccessTime { get; set; }
            public DateTime fileLastWriteTime { get; set; }
            public string fileLength { get; set; }
        }
        private void loadList()
        {
            string fileName = nameText.Text;
            // 设置要列出文件的文件夹路径
            string folderPath = "F:\\vue脚手架项目\\小杨工具库\\VUE-API\\YYSService\\SQL Script";
            var list = new List<fileClass>();
            // 获取指定文件夹中的所有文件，包括子文件夹中的文件
            string[] fileEntries = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
            foreach (string filePath in fileEntries)
            {
                FileInfo file = new FileInfo(filePath);
                if (file.Length == 0) continue;
                if (!file.Name.Contains(fileName)) continue;
                list.Add(new fileClass
                {
                    fileName = file.Name,//文件名称
                    fileFullName = file.FullName,//完整路径
                    fileCreationTime = file.CreationTime,//创建时间
                    fileLastAccessTime = file.LastAccessTime,//最后访问时间
                    fileLastWriteTime = file.LastWriteTime,//修改时间
                    fileLength = GetFileSize(file.Length)//文件大小: 
                });
            }
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 15, FontStyle.Bold);
            //dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.Purple;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridView1.DefaultCellStyle.Font = new Font("宋体", 12);

            dataGridView1.DataSource = list.OrderByDescending(x=> x.fileLastWriteTime).ToList();

        }
        private void button1_Click(object sender, EventArgs e)
        {
            loadList();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            dataGridView1.Height = (int)(this.ClientRectangle.Height * 0.9);
        }
    }
}
